package com.bnz.controller;

import com.bnz.Student;
import com.bnz.service.StudentService;
import com.bnz.until.PageResult;
import com.bnz.until.R;
import com.bnz.vo.StudentVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("stu")
public class StudentController {
    // 引入service
    private StudentService studentService;

    // 1、查找所有学生
    @GetMapping("list")
    public List<Student> findAll(){
        List<Student> all = studentService.findAll();
        return all;
    }

    // 2、添加学生
    @PostMapping("add")
    // @RequestBody:接收前端传给后端的json数据
    public String add(@RequestBody Student student){
        studentService.add(student);
        return "add";
    }

    // 3、获取vue页面需要的学生列表
    @GetMapping("findAll")
    public R findAll1(){
        return R.ok().data("list",studentService.findAll());
    }

    // 4、分页查询
    @GetMapping("findByPage/{page}/{pagesize}")
    // @PathVariable 映射 URL 绑定的占位符
    public R findPage(@PathVariable("page") Integer page, @PathVariable("pagesize") Integer pagesize){
        // 4.1、分页查询，查出分页对象
        PageResult byPage = studentService.findByPage(page, pagesize);
        return R.ok().data("pr",byPage);
    }

    // 5、条件查询带分页
    @PostMapping("search/{page}/{pagesize}")
    // @PathVariable 映射 URL 绑定的占位符
    public void search(@PathVariable("page") Integer page, @PathVariable("pagesize") Integer pagesize, @RequestBody StudentVo studentVo){
        // 5.1、分页查询对象（条件搜索）
        studentService.search(page,pagesize,studentVo);
    }

}
